var wxappid;
var wxtimestamp;
var wxnonceStr;
var wxsignature;

//验证微信接口签名
function wxcheckWXSignature(){
	var openId=document.getElementById("openId").value;
	var xhr = createXmlHttpRequest2();
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				var json = xhr.responseText;
				var jsonData = eval("(" + json + ")");//转换后的JSON对象
				wxappid = jsonData.appId;
				wxtimestamp = jsonData.timestamp;
				wxnonceStr = jsonData.nonceStr;
				wxsignature = jsonData.signature;
				
				try{
				    wxinitapi();
				    wxcloseMenu();
				}catch(err){
					
				}
			}
		}
	};
	var uri=encodeURIComponent(window.location.href);
	var url = "/api/ajax/getWeixinSignatureData?openId="+ openId + "&url=" + uri;
	xhr.open("post", url, true);
	xhr.send();
}


function wxcheckWXSignature2(){
	var openId=document.getElementById("openId").value;
	var xhr = createXmlHttpRequest2();
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				var json = xhr.responseText;
				var jsonData = eval("(" + json + ")");//转换后的JSON对象
				wxappid = jsonData.appId;
				wxtimestamp = jsonData.timestamp;
				wxnonceStr = jsonData.nonceStr;
				wxsignature = jsonData.signature;
				
				try{
				    wxinitapi();
				}catch(err){
					
				}
			}
		}
	};
	var uri=encodeURIComponent(window.location.href);
	var url = ctx +  "/api/ajax/getWeixinSignatureData?openId="+ openId + "&url=" + uri;
	xhr.open("post", url, true);
	xhr.send();
}

function wxcloseMenu(){
	wx.ready(function () {
		wx.hideOptionMenu();
	});
}
//获取XmlHttpRequest对象
function createXmlHttpRequest2() {
	if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
}

function wxinitapi(){
	 wx.config({
	      debug: false,
	      appId: wxappid,
	      timestamp: wxtimestamp,
	      nonceStr: wxnonceStr,
	      signature: wxsignature,
	      jsApiList: [
	        'onWXDeviceBindStateChange',
	        'onWXDeviceStateChange',
	        'onScanWXDeviceResult',
	        'onReceiveDataFromWXDevice',
	        'onWXDeviceBluetoothStateChange',
	        'openWXDeviceLib',
	        'closeWXDeviceLib',
	        'getWXDeviceInfos',
	        'getWXDeviceTicket',
	        'setSendDataDirection',
	        'startScanWXDevice',
	        'stopScanWXDevice',
	        'connectWXDevice',
	        'disconnectWXDevice',
	        'sendDataToWXDevice',
	        'scanQRCode',
	        'configWXDeviceWiFi',
	        'onMenuShareTimeline',
	        'onMenuShareAppMessage',
	        'onMenuShareQQ',
	        'onMenuShareWeibo',
	        'hideMenuItems',
	        'showMenuItems',
	        'hideAllNonBaseMenuItem',
	        'showAllNonBaseMenuItem',
	        'translateVoice',
	        'playMusic',
	        'startRecord',
	        'stopRecord',
	        'onRecordEnd',
	        'playVoice',
	        'pauseVoice',
	        'stopVoice',
	        'uploadVoice',
	        'downloadVoice',
	        'chooseImage',
	        'previewImage',
	        'uploadImage',
	        'downloadImage',
	        'getNetworkType',
	        'openLocation',
	        'getLocation',
	        'hideOptionMenu',
	        'showOptionMenu',
	        'closeWindow',
	        'scanQRCode',
	        'chooseWXPay',
	        'getLatestAddress',
	        'editAddress',
	        'openProductSpecificView',
	        'addCard',
	        'chooseCard'
	      ]
	  });
}